package com.dream.database;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * Description: database super helper
 * author: WangKunHui
 * date: 11/28/16 2:32 PM
 * <p>
 * Copyright©2016 by wang. All rights reserved.
 */
public abstract class BaseDatabaseHelper extends OrmLiteSqliteOpenHelper {

    private String TAG = "BaseDatabaseHelper";

    public BaseDatabaseHelper(Context context, String dbName) {

        super(context, dbName, null, DBConfig.getDatabaseVersion());
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource) {
        try {
            createTables(getTables());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    protected abstract List<Class> getTables();


    private void createTables(List<Class> classList) throws SQLException {
        for (Class clazz : classList) {
            Log.i(TAG, "创建表  ：  " + clazz.getSimpleName());
            TableUtils.createTableIfNotExists(connectionSource, clazz);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {

            Log.i(TAG, "onUpgrade");
            if (oldVersion < newVersion) {
                updateTables();
                onCreate(sqLiteDatabase, connectionSource);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void updateTables() {

    }

    /**
     * update table
     *
     * @param db           database instance
     * @param tableName    tableName
     * @param columnName
     * @param columnType
     * @param defaultField
     * @throws SQLException
     */
    protected synchronized void updateColumn(SQLiteDatabase db, String tableName, String columnName, String columnType, Object defaultField) throws SQLException {
        if (db != null) {
            Cursor c = db.rawQuery("SELECT * from " + tableName + " limit 1 ", null);
            boolean flag = false;

            if (c != null) {
                for (int i = 0; i < c.getColumnCount(); i++) {
                    if (columnName.equalsIgnoreCase(c.getColumnName(i))) {
                        flag = true;
                        break;
                    }
                }

                if (flag == false) {
                    if (defaultField != null) {
                        String sql = "alter table " + tableName + " add "
                                + columnName + " " + columnType + " default "
                                + defaultField;
                        db.execSQL(sql);
                    } else {
                        String sql = "alter table " + tableName + " add "
                                + columnName + " " + columnType;
                        db.execSQL(sql);
                    }
                }
                c.close();
            }
        }
    }
}
